<?php

namespace App\Models;

use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class JrttEquipment extends Model
{
    //
    protected $table= "jrtt_equipment";
    protected $guarded = [];

    public static function notify($os,$imei,$idfa){
        $client = new Client();
        if ($os=='android'){
            $jrtt = JrttEquipment::where('os',0)->where("imei",md5($imei))->first();
            if ($jrtt) {
                $url = $jrtt->callback_url.'?imei='.$jrtt->imei.'&idfa='.$jrtt->idfa.'&conv_time='.Carbon::now().'&event_type=0';
                $res = $client->request('GET', $url);
                $data = json_decode($res->getBody(), true);
                if ($data['ret'] == 0){
                    $jrtt->is_match = 1;
                    $jrtt->match_time = Carbon::now();
                    $jrtt->save();
                }
            }
        }elseif($os=='ios'){
            $jrtt = JrttEquipment::where('os',1)->where("idfa",$idfa)->first();
            if ($jrtt) {
                $url = $jrtt->callback_url.'?imei='.$jrtt->imei.'&idfa='.$jrtt->idfa.'&conv_time='.Carbon::now().'&event_type=0';
                $res = $client->request('GET', $url);
                $data = json_decode($res->getBody(), true);
                if ($data['ret'] == 0) {
                    $jrtt->is_match = 1;
                    $jrtt->match_time = Carbon::now();
                    $jrtt->save();
                }
            }
        }
    }
}
